package com.swx.identityservice.configs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.ApiKeyVehicle;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.MultipartConfigElement;
import java.nio.file.Paths;

import static com.google.common.collect.Lists.newArrayList;

/**
 * Created by SCBP
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.swx.identityservice.webapis"))
            .paths(PathSelectors.any())
            .build()
            .securitySchemes(newArrayList(apiKey()));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("identityserver-service项目API文档")
            .description("来自identityserver-service")
            .termsOfServiceUrl("http://localhost:8081/")
            .version("1.0")
            .build();
    }

    private ApiKey apiKey() {
        return new ApiKey("Authorization", "api_key", "header");
    }


    @Bean
    SecurityConfiguration security() {
        return new SecurityConfiguration((String) null, (String) null, (String) null, (String) null, (String) null, ApiKeyVehicle.HEADER, "Authorization", ",");
    }

    /**
     * @author Administrator
     * @version V1.0.0
     * @date 2018/5/20 0020 1:02
     */
    @Controller
    public static class HomeController {

        private final ResourceLoader resourceLoader;

        @Autowired
        public HomeController(ResourceLoader resourceLoader) {
            this.resourceLoader = resourceLoader;
        }

        @RequestMapping(method = RequestMethod.GET, value = "/{yyyy}-{MM}-{dd}/{filename:.+}")
        @ResponseBody
        public ResponseEntity<?> getFile(@PathVariable String yyyy, @PathVariable String MM, @PathVariable String dd, @PathVariable String filename) {
            try {
                return ResponseEntity.ok(resourceLoader.getResource(
                    "file:" + Paths.get("c:/upload/" + yyyy+"-"+MM+"-"+dd + "/", filename).toString()));
            } catch (Exception e) {
                return ResponseEntity.notFound().build();
            }
        }

    }

    /**
     * @author Administrator
     * @version V1.0.0
     * @date 2018/5/19 0019 21:43
     */
    @Configuration
    public static class WebAppConfig extends WebMvcConfigurerAdapter {

        /**
         * 在配置文件中配置的文件保存路径
         */
    //    @Value("${img.location}")
    //    private String location;

        @Bean
        public MultipartConfigElement multipartConfigElement(){
            MultipartConfigFactory factory = new MultipartConfigFactory();
            //文件最大KB,MB
            factory.setMaxFileSize("2MB");
            //设置总上传数据总大小
            factory.setMaxRequestSize("10MB");
            return factory.createMultipartConfig();
        }


    }
}
